<template>
	<!-- 问题：
	1.vuex缺点：无法持久存储数据；页面一刷新，数据丢失；
	2.vuex存储数据就是在内存中设置一个对象，浏览器一旦刷新，对象需要重新建立，成为初始状态值；
	-->
	<el-card class="box-card">
		<div slot="header" class="clearfix">
			<span>账户资料</span>
		</div>
		<el-form :model="form" :rules="rules" ref="form" label-width="100px">
			<el-form-item label="用户名" prop="username">
				<el-input v-model="form.username" disabled></el-input>
			</el-form-item>
			<el-form-item label="姓名" prop="fullname">
				<el-input v-model="form.fullname"></el-input>
			</el-form-item>
			<el-form-item label="性别" prop="sex">
				<el-radio-group v-model="form.sex">
					<el-radio label="男"></el-radio>
					<el-radio label="女"></el-radio>
				</el-radio-group>
			</el-form-item>
			<el-form-item label="手机号" prop="tel">
				<el-input v-model="form.tel"></el-input>
			</el-form-item>
			<el-form-item label="邮箱" prop="email">
				<el-input v-model="form.email"></el-input>
			</el-form-item>
			<el-form-item label="头像" prop="avatar">
				<UploadPhoto v-model="form.avatar" type="avatar"></UploadPhoto>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="handleEdit">保存修改</el-button>
			</el-form-item>
		</el-form>
	</el-card>
</template>

<script>
import { mapState, mapActions } from "vuex";
import UploadPhoto from "@/components/UploadPhoto.vue";
export default {
	name: 'Account',
	components: { UploadPhoto },
	data() {
		return {
			form: {
				user_id: '',
				username: '',
				fullname: '',
				role_id: '',
				sex: '',
				tel: '',
				email: '',
				role_name: '',
				avatar: '',
			},
			headers: { Authorization: `Bearer ${sessionStorage.access_token}` },
			rules: {
				fullname: [{ required: true, message: "请填写姓名！", trigger: 'blur' }],
				tel: [
					{ required: true, message: "请填写姓名！", trigger: 'blur' },
					{
						pattern: /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
						message: '请填写合规的手机号！',
						trigger: 'blur'
					}
				],
				email: [
					{ required: true, message: "请填写姓名！", trigger: 'blur' },
					{
						pattern: /^[A-Za-z0-9]+([_\.][A-Za-z0-9]+)*@([A-Za-z0-9\-]+\.)+[A-Za-z]{2,6}$/,
						message: '请填写合规的网址！',
						trigger: 'blur'
					}
				],
				avatar: [{ required: true, message: "请上传头像！", trigger: 'blur' },],
			}
		}
	},
	mounted() {
		document.title = "账户信息";
		// 还原表单
		this.form = { ...this.profile };
	},
	computed: {
		// 取出的数据不能直接绑到上面的表单中，那样会双向数据绑定，从而修改state中的数据！
		...mapState('account', ['profile']),
	},
	methods: {
		...mapActions('account', ['edit_info']),
		handleEdit() {
			this.$refs.form.validate(async (valid) => {
				if (valid) {
					let { status, msg } = await this.edit_info({ ...this.form });
					if (status) {
						this.$message.success(msg);
					} else {
						this.$message.error(msg);
					}
				}
			});
		},
	},
};

</script>

<style>
.avatar-uploader .el-upload {
	border: 1px dashed #d9d9d9;
	border-radius: 6px;
	cursor: pointer;
	position: relative;
	overflow: hidden;
}

.avatar-uploader .el-upload:hover {
	border-color: #409EFF;
}

.avatar-uploader-icon {
	font-size: 28px;
	color: #8c939d;
	width: 178px;
	height: 178px;
	line-height: 178px;
	text-align: center;
}

.avatar {
	width: 178px;
	height: 178px;
	display: block;
}
</style>
